﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Context;
using Entities;
namespace WCF_ListenMusic.Services.Module_Singer.Public
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SingerService" in code, svc and config file together.
    public class SingerService : ISingerService
    {
        Context.Connect_DBDataContext context = new Context.Connect_DBDataContext();

        public List<Singer> getAllSinger_byCountry(int countryid)
        {
            try {
                var q = from a in context.Singers
                        where a.CountryID.Equals(countryid) && a.Display.Equals(true)
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public List<ViewSearch_SingerName> search_SongName(string keyword)
        {
            try
            {
                var q = from a in context.ViewSearch_SingerNames
                        join b in context.search_SingerName(keyword) on a.SingerID equals b.KEY                       
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public List<Singer> getAllSinger()
        {
            try
            {
                var q = from a in context.Singers
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public Singer checkSingerName(string singername)
        {
            context.CommandTimeout = 3600;
            try
            {
                var q = from a in context.Singers
                        where a.SingerName.Trim().ToLower().Equals(singername.Trim().ToLower())
                        select a;
                return q.SingleOrDefault();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public int getMaxID_Singer()
        {
            context.CommandTimeout = 3600;
            try
            {
                var q = (from a in context.Singers                        
                        select a.SingerID).Max();
                return q;
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool insertNewSinger(Singer singer)
        {
            context.CommandTimeout = 3600;
            try
            {
                singer.SingerID = getMaxID_Singer() + 1;
                context.Singers.InsertOnSubmit(singer);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }


        public Singer getSinger_bySingerName(string singerName)
        {
            try
            {
                var q = from a in context.Singers
                        where a.SingerName.Trim().ToLower().Equals(singerName.Trim().ToLower())
                        select a;
                return q.SingleOrDefault();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }
    }
}
